package model;

import java.util.HashSet;
import java.util.Set;
import vinciappli.CardId;


public class Player {

    private final String nickname;
    private String urlAvatar;
    /* Total amount of troops owned */
    private int nbTroops = 0;
    /* Amount of troops who are not placed on the gameboard */
    private int availableTroops = 0;
    /* Amount of forts the player can placed on his empire, thanks to card "forts" */
    private int availableForts = 0;
    /* Total amount of points */
    private int points = 0;
    private Set<Empire> empires = new HashSet<Empire>();
    /* Computer or real player */
    private boolean isComputer;
    /* Total times per turn a player can use card "espions" advantage */
    private int canSpy = 0;
    /* Selected player who can't attack or be attacked during a turn */
    private Player friend;

    public Player(String nickname, boolean isComputer) {
        this.nickname = nickname;
        this.isComputer = isComputer;
    }

    public Player(String nickname, String urlAvatar, boolean isComputer)
    {
        this.nickname = nickname;
        this.urlAvatar = urlAvatar;
        this.isComputer = isComputer;
    }

    /**
     * Check if a player can cross sea areas thanks to the card "astronomie"
     * @return true of false
     */
    public boolean canGoThroughSeaArea () {
        if(this.getEmpire(false).getCivilization().isComposedOf(CardId.astronomie))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public Set<Empire> getEmpires () {
        return empires;
    }

    /**
     * Retrieve empire from the player
     * @param thisIsDepressed : true if the desired empire is depressed, false for the active empire
     * @return the active or the depressed empire of the player
     */
    public Empire getEmpire(boolean thisIsDepressed)
    {
        for(Empire e : empires)
        {
            if(!(e.isDepressed()) && !(thisIsDepressed))        // Return the active empire
            {
                return e;
            }
            if(e.isDepressed() && thisIsDepressed)              // Return the depressed empire
            {
                return e;
            }
        }
        return null;
    }

    public void leaveProvince(Province p)
    {
        for(Empire e : empires)
        {
            e.getListProvinces().remove(p);
        }
    }

    public void setPointsFromCurrentValue(int val)
    {
        points += val;
    }

    /**
     * Change the total number of troops possessed by the player
     * @param num : added or subtracted number (could be negative or positive number)
     */
    public void setTroopsFromCurrentNumber(int num)
    {
        nbTroops += num;
    }

    /**
     * Change the number of troops who are immediately ready to conquer a province (not placed on the gameboard yet)
     * @param num : added or subtracted number (could be negative or positive number)
     */
    public void setAvailableTroopsFromCurrentNumber(int num)
    {
        availableTroops += num;
    }
    
   
    /***************************** GETTERS & SETTERS **************************/

    public String getNickname () {
        return nickname;
    }

    public void setEmpires (Set<Empire> val) {
        this.empires = val;
    }

    public int getNbTroops () {
        return nbTroops;
    }

    public void setNbTroops (int val) {
        this.nbTroops = val;
    }

    public int getPoints () {
        return points;
    }

    public void setPoints (int val) {
        this.points = val;
    }

    public int getAvailableTroops() {
        return availableTroops;
    }

    public void setAvailableTroops(int availableTroops) {
        this.availableTroops = availableTroops;
    }

    public boolean isComputer() {
        return isComputer;
    }

    public void setAvailableForts(int availableForts) {
        this.availableForts = availableForts;
    }

    public boolean canSpy() {
        if(canSpy > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void setSpyTimes(int times) {
        this.canSpy = times;
    }

    public void setSpyTimesFromCurrentValue(int val) {
        this.canSpy += val;
    }

    public Player getFriend() {
        return friend;
    }

    public void setFriend(Player friend) {
        this.friend = friend;
    }

    public String getUrlAvatar()
    {
        return urlAvatar;
    }

}

